package org.example.volunteerend.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.example.volunteerend.mapper.SearchMapper;
import org.example.volunteerend.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchServiceImpl implements SearchService {

    private final SearchMapper searchMapper;

    @Autowired
    public SearchServiceImpl(SearchMapper searchMapper) {
        this.searchMapper = searchMapper;
    }

    @Override
    public Map<String, Object> search(String keyword, String type, Integer page, Integer pageSize) {
        // 设置默认值
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        if (type == null || type.isEmpty()) {
            type = "all";
        }
        
        Map<String, Object> result = new HashMap<>();
        
        // 根据搜索类型，调用相应的搜索方法
        if ("all".equals(type) || "projects".equals(type)) {
            result.put("projects", searchProjects(keyword, page, pageSize));
        }
        
        if ("all".equals(type) || "users".equals(type)) {
            result.put("users", searchUsers(keyword, page, pageSize));
        }
        
        return result;
    }

    @Override
    public Map<String, Object> searchProjects(String keyword, Integer page, Integer pageSize) {
        // 设置默认值
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        
        // 查询项目
        List<Map<String, Object>> projects = searchMapper.searchProjects(keyword, offset, pageSize);
        
        // 处理日期时间格式
        for (Map<String, Object> project : projects) {
            if (project.get("start_time") != null) {
                project.put("start_time", project.get("start_time").toString());
            }
            if (project.get("end_time") != null) {
                project.put("end_time", project.get("end_time").toString());
            }
        }
        
        // 统计总数
        int total = searchMapper.countSearchProjects(keyword);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("rows", projects);
        
        return result;
    }

    @Override
    public Map<String, Object> searchUsers(String keyword, Integer page, Integer pageSize) {
        // 设置默认值
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }
        
        // 计算偏移量
        int offset = (page - 1) * pageSize;
        
        // 查询用户
        List<Map<String, Object>> users = searchMapper.searchUsers(keyword, offset, pageSize);
        
        // 统计总数
        int total = searchMapper.countSearchUsers(keyword);
        
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("rows", users);
        
        return result;
    }
} 